import Cookie from 'js-cookie'

export default {
  isEmpty (v) {
    return v === null || v === undefined || v.toString().trim() === ''
  },
  getQueryString (name) {
    var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i')
    var r = window.location.search.substr(1).match(reg)
    if (r != null) {
      return decodeURIComponent(r[2])
    }
    return null
  },
  getLocalStorage (key) {
    let v = null
    if (window.localStorage) {
      v = window.localStorage.getItem(key)
    }
    if (this.isEmpty(v)) {
      v = Cookie.get(key)
    }
    return v
  },
  saveLocalStorage (key, item, expires = 30) {
    if (window.localStorage) {
      window.localStorage.setItem(key, item)
    } else {
      Cookie.set(key, item, {
        expires: expires,
        path: '/'
      })
    }
  },
  clearLocalStorage () {
    if (window.localStorage) {
      window.localStorage.clear()
    }
    let cookies = Cookie.get()
    for (let key of Object.keys(cookies)) {
      Cookie.remove(key)
    }
  }
}
